<?php
/**
 * 公共接口
 * @return
 * @author yuncran@126.com
 */

namespace app\api\controller;

use app\BaseController;
use app\CacheKey;
use app\model\AreaModel;
use app\model\ConfigModel;
use app\Request;
use think\facade\Db;
use think\facade\Lang;

class Common extends BaseController
{

    /**
     * 获取系统配置
     * @return void
     * @author yuncran@126.com
     */
    public function getConfig()
    {
        $data = ConfigModel::where([
            [
                'group', 'in', ['basic','example']
            ]
        ])->field(['id', 'title', 'value'])->select();
        return $this->renderSuccess($data, lang('查询成功'));
    }



    /**
     * 获取支付方式
     * @return void
     * @author yuncran@126.com
     */
    public function getPayConfig()
    {
        $data = ConfigModel::where([
            [
                'group', 'in', ['example']
            ]
        ])->where('value',1)->field(['id', 'title', 'value'])->select();
        
        
        $paypal = ConfigModel::where('id',32)->where('value', 1)->value('value');
        $usdt = ConfigModel::where('id',34)->where('value', 1)->value('value'); 
        
        $user = Db::name('user')->where('id',  $this->uid)->field('id,is_paypal,is_usdt')->find();
        
        $payPalValue = 1;
        $usdtValue = 1;
        if($user){
            $payPalValue = $paypal == 1 && $user['is_paypal'] ==1 ? 1 : 0;
            $usdtValue = $usdt == 1 && $user['is_usdt'] ==1 ? 1 : 0;            
        }
        $data = [
            'config' => $data,
            'user' => [
                ['value' => $payPalValue, 'title' => 'paypal'],
                ['value' => $usdtValue, 'title' => 'usdt'],
            ]];
        return $this->renderSuccess($data, lang('查询成功'));
    }

    /**
     * 用户协议
     * @return void
     * @author yuncran@126.com
     */
    public function getAgreement(Request $request)
    {
        $param = $request->param(['id' => 1]);
        $param['id'] = empty($param['id']) ? $request->get(['id' => 1]) : $param['id'];
        $data = \app\model\AgreementModel::where('id', $param['id'])->field(['id', 'create_time', field('title')])->find();
        return $this->renderSuccess($data, lang('查询成功'));
    }

    /**
     * 获取省市区
     * @return void
     * @author yuncran@126.com
     */
    public function getArea(Request $request)
    {
        $param = $request->param();
        $data = AreaModel::where(function ($query) use ($param) {
            if (empty($param)) {
                $query->where('pid', 0);
            } else {
                $pid = AreaModel::where('shortname', $param['pid'])->value('id');
                $query->where('pid', $pid);
            }
        })->cache(true, 1)->paginate()->each(function ($item) {
            return $item;
        });
        return $this->renderSuccess($data);
    }


    /**
     * 获取国家地址
     * @return void
     * @author yuncran@126.com
     */
    public function getCountry()
    {
        $data = Db::table('a_countries')
            ->field(['id', 'name', 'native', 'capital'])
            ->select();
        return $this->renderSuccess($data);
    }

    /**
     * 获取城市
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @author yuncran@126.com
     */
    public function getProvince(Request $request)
    {
        $param = $request->param();
        $data = Db::table('a_states')->where($param)
            ->field(['id', 'name', 'country_id', 'country_code'])
            ->select();
        return $this->renderSuccess($data);
    }

    /**
     * 市
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @author yuncran@126.com
     */
    public function getCity(Request $request)
    {
        $param = $request->param();
        $data = Db::table('a_cities')->where($param)
            ->field(['name', 'country_id', 'country_code'])
            ->select();
        return $this->renderSuccess($data);
    }


    /**
     * 获取系统敏感词
     * @return void
     * @author yuncran@126.com
     */
    public function getSensitive()
    {
        $data = ConfigModel::getSensitive(['id' => 25]);
        return $this->renderSuccess($data);
    }

}